package com.soft.line.controller;

import com.soft.line.aop.Permission;
import com.soft.line.entity.UserInfo;
import com.soft.line.service.UserInfoService;
import com.soft.line.util.exception.CommonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * @author wu.xueqiao on 2020/5/22.
 * @version 0.1
 */
@Api(description = "用户管理相关接口")
@RestController
@RequestMapping("/systemAdmin")
public class UserInfoController {


    @Autowired
    private UserInfoService userInfoService;

    /**
     * 添加&&修改用户
     * @param userInfo
     * @return
     */
    @ApiOperation(value = "添加&&修改用户", notes = "添加&&修改用户")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "userInfo", value = "账号", required = false, dataType = "com.soft.line.entity.UserInfo"),
    })
    @PostMapping("/account")
    public CommonResult addUser(@RequestBody @Validated UserInfo userInfo){
        int result=userInfoService.addUser(userInfo);
        if(result==1){
            return CommonResult.success("保存成功");
        }else {
            return CommonResult.failed();
        }
    }

    @ApiOperation(value = "获取用户列表&&模糊查询", notes = "获取用户列表")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "pageSize", value = "页量", required = false, dataType = "java.lang.Integer"),
            @ApiImplicitParam(paramType = "query", name = "pageNum", value = "页码", required = false, dataType = "java.lang.Integer"),
            @ApiImplicitParam(paramType = "query", name = "map", value = "模糊匹配条件", required = false, dataType = "java.util.Map")
    })
    @PostMapping("/accounts")
    public CommonResult getAllUsers(@RequestBody Map<String,Object> map){
        return CommonResult.success(userInfoService.getAllUsers(map));
    }

    /**
     * @Permission
     * @Des   权限判定该 删除操作者 是否具有该权限
     * @param map
     * @return
     */
    @ApiOperation(value = "删除用户", notes = "删除用户")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "id", value = "userId", required = true, dataType = "java.lang.Integer"),
    })
    //@Permission
    @DeleteMapping("/account")
    public CommonResult delUser(@RequestBody Map<String,Object> map){
        int result=userInfoService.delUser(Integer.valueOf(map.get("id").toString()));
        if(result==1){
            return CommonResult.success("删除成功");
        }else {
            return CommonResult.failed();
        }
    }
    @ApiOperation(value = "获取单个用户信息", notes = "获取用户信息")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "id", value = "userId", required = false, dataType = "java.lang.Integer"),
    })
    @GetMapping("/accounts")
    public CommonResult getUserInfo(@RequestParam("id") int id){
        return CommonResult.success(userInfoService.getUserInfo(id));
    }

    @ApiOperation(value = "修改用户密码", notes = "修改用户密码")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "id", value = "userId", required = true, dataType = "java.lang.Integer"),
            @ApiImplicitParam(paramType = "query", name = "oldPassword", value = "oldPassword", required = true, dataType = "java.lang.String"),
            @ApiImplicitParam(paramType = "query", name = "newPassword", value = "newPassword", required = true, dataType = "java.lang.String"),
            @ApiImplicitParam(paramType = "query", name = "confirmPassword", value = "confirmPassword", required = true, dataType = "java.lang.String")
    })
    @PostMapping("/changePassword")
    public CommonResult changePassword(@RequestBody Map<String,Object> map){
        Boolean result=userInfoService.changePassword(map);
        if(result){
            return CommonResult.success("修改成功");
        }else {
            return CommonResult.failed();
        }
    }
}
